package manage.controller;

import manage.dao.UserMapper;
import manage.model.FileType;
import manage.model.User;
import manage.service.impl.FileTypeServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class FileTypeController {
    @Autowired
    private FileTypeServiceImpl fileTypeService;

    @Autowired
    private UserMapper userMapper;

    private User user;
    //返回FileType的list
    @RequestMapping(value = "/fileType")
    @ResponseBody
    public Msg FileTypeData(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",fileTypeService.selectAll(page,limit));
    }
    @RequestMapping("/fileTypeList")
    @ResponseBody
    public Msg FileTypeData(){
        return Msg.success().add("data",fileTypeService.selectAll());
    }

    //添加FileType
    @ResponseBody
    @RequestMapping(value = "/addFileType",method = RequestMethod.POST)
    public Msg addFileType(FileType fileType){

        String s= fileTypeService.insert(fileType);
        LogUtil.writeFileLog(user.getAccount(),"添加文件类型:"+fileType.toString());
        return Msg.attendanceAdd(s);
    }

    //修改FileType
    @ResponseBody
    @RequestMapping(value = "/addFileType",method = RequestMethod.PUT)
    public Msg updateFileType(FileType FileType){

        String s= fileTypeService.update(FileType);
        LogUtil.writeFileLog(user.getAccount(),"修改文件类型-改:"+FileType.toString());
        return Msg.attendanceAdd(s);
    }
    //删除FileType
    @ResponseBody
    @RequestMapping("/delFileType")
    public Msg delFileType(@RequestBody Integer id){
        String s = fileTypeService.del(id);
        LogUtil.writeFileLog(user.getAccount(),"删除文件类型:"+id.toString());
        return Msg.attendanceAdd(s);
    }

    //根据主键查FileType 和修改配合使用
    @ResponseBody
    @RequestMapping("/fileTypeSelect")
    public Msg selFileType(@RequestBody Integer id){
        FileType fileType = fileTypeService.select(id);
        LogUtil.writeFileLog(user.getAccount(),"修改文件类型-查:"+fileType.toString());
        return Msg.success().add("fileType",fileType);
    }
}
